// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "customerId": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ]
  },
  "productId": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "limit": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "贷款额度",
    "label": "贷款额度"
  },
  "loanPeriod": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "12",
            "text": "12"
          },
          {
            "value": "24",
            "text": "24"
          },
          {
            "value": "36",
            "text": "36"
          },
          {
            "value": "48",
            "text": "48"
          },
          {
            "value": "60",
            "text": "60"
          },
          {
            "value": "120",
            "text": "120"
          },
          {
            "value": "240",
            "text": "240"
          },
          {
            "value": "360",
            "text": "360"
          }
        ]
      }
    ],
    "title": "贷款期限(月)",
    "label": "贷款期限(月)"
  },
  "agreementDocs": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "合同上传",
    "label": "合同上传"
  },
  "remark": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "备注",
    "label": "备注"
  },
  "belongTo": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "归属人",
    "label": "归属人"
  },
  "lastUpdateBy": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "最近修改人",
    "label": "最近修改人"
  },
  "belongDept": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "归属部门",
    "label": "归属部门"
  },
  "updateTime": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "最近修改时间",
    "label": "最近修改时间"
  }
}

const enumConverter = {
  "loanPeriod_valuetotext": {
    "12": "12",
    "24": "24",
    "36": "36",
    "48": "48",
    "60": "60",
    "120": "120",
    "240": "240",
    "360": "360"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
